﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;
using System.Web.Security;

namespace BitPortal.Business
{
    public static partial class NoteBLL
    {
        public static string[] RolesInOrder = new string[] { "Customer", "Employee", "Admin", "Ro2otAdmin" };
        public static int Delete(long noteID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            Note n = bitEnt.Notes.SingleOrDefault(ent => ent.NoteID == noteID);
            if (n != null)
            {
                bitEnt.Notes.DeleteObject(n);
            }
            return bitEnt.SaveChanges();
        }

        public static int Insert(this Note note)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            bitEnt.Notes.AddObject(note);
            NoteGroup ng = bitEnt.NoteGroups.SingleOrDefault(ent => ent.NoteGroupID == note.NoteGroupID);
            ng.LastNoteDate = note.Logs.DatetimeCreated;
            return bitEnt.SaveChanges();
        }

        public static IQueryable<Note> GetAll()
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.Notes;
        }

        public static Note GetByID(long noteID)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            return bitEnt.Notes.SingleOrDefault(ent => ent.NoteID == noteID);
        }

        /// <summary>
        /// Check quyền của người dùng hiện tại có thể sửa Note?
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public static bool CheckPermissonToEdit(this Note note)
        {
            MembershipUser createUser = Membership.GetUser(note.Logs.CreatingUser);
            int r1 = Membership.GetUser().GetHighestRole();
            int r2 = createUser.GetHighestRole();
            if (r1 > r2) return true;
            if (r1 == r2) return (createUser.UserName == Membership.GetUser().UserName);
            return false;
        }

        /// <summary>
        /// Check quyền của người dùng hiện tại có thể xóa Note?
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public static bool CheckPermissonToDelete(this Note note)
        {
            MembershipUser createUser = Membership.GetUser(note.Logs.CreatingUser);
            int r1 = Membership.GetUser().GetHighestRole();
            int r2 = createUser.GetHighestRole();
            if (r1 > r2 && r1 >= 2) return true;
            if (r1 == r2) return (createUser.UserName == Membership.GetUser().UserName);
            return false;
        }

        public static int GetHighestRole(this MembershipUser user)
        {
            var roles = Roles.GetRolesForUser(user.UserName);
            for (int i = RolesInOrder.Length - 1; i >= 0; i--)
            {
                if (roles.Contains(RolesInOrder[i]))
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// Lay ra danh sach ghi chu theo dieu kien
        /// </summary>
        /// <param name="nodeGroupID"></param>
        /// <param name="isEnabled"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public static List<Note> GetByCondition(long nodeGroupID = 0, bool? isEnabled = null, string role = null)
        {
            return GetAll().Where(ent =>
                (isEnabled == null || ent.IsEnabled == isEnabled) &&
                (nodeGroupID == 0 || ent.NoteGroupID == nodeGroupID) &&
                (role == null || ent.AllowRolesView.Contains(role))
                ).ToList();

        }

        /// <summary>
        /// Lay ra danh sach ghi chu trong quyen view cua nguoi dung
        /// </summary>
        /// <param name="nodeGroupID"></param>
        /// <param name="isEnabled"></param>
        /// <returns></returns>
        public static List<Note> GetListNodeToView(long nodeGroupID = 0, bool? isEnabled = null)
        {
            int r = Membership.GetUser().GetHighestRole();
            string role = RolesInOrder[r];
            Guid? userkey = BitContext.Current.LoginUser;
            if (r < 0 || r > 3) return GetAll().Where(ent =>
                (isEnabled == null || ent.IsEnabled == isEnabled) &&
                (nodeGroupID == 0 || ent.NoteGroupID == nodeGroupID) &&
                (ent.Logs.CreatingUser == (userkey))
                ).ToList();
            return GetAll().Where(ent =>
                (isEnabled == null || ent.IsEnabled == isEnabled) &&
                (nodeGroupID == 0 || ent.NoteGroupID == nodeGroupID) &&
                (ent.Logs.CreatingUser == (userkey) || ent.AllowRolesView.Contains(role))
                ).ToList();
        }
    }
}